package com.teamwe.movecard.service;

import com.teamwe.movecard.domain.Problem;
import com.teamwe.movecard.repository.ProblemPageRepository;
import com.teamwe.movecard.repository.ProblemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProblemService {

    @Autowired
    private ProblemRepository problemRepo;

    @Autowired
    private ProblemPageRepository problemPageRepo;

    /**
     * 随机获取题库中对应年级中的10道题
     * @param grade
     * @return
     */
    public List<Problem> getRandomProblemList(String grade){
        return (List<Problem>)problemRepo.getRandomProblemList(grade);
    }

    /**
     * 添加或者保存一道题目
     * @param problem
     */
    public Problem  saveProblelm(Problem problem){
        return problemRepo.save(problem);
    }

    /**
     * 根据年级信息分页查询，如果年级信息为null的话，直接返回不分年级信息的分页查询结果
     * @param pageSize
     * @param pageNum
     * @return
     */
    public Page<Problem> getProblemByPage(String grade ,Integer pageSize, Integer pageNum){
        Pageable pageable = PageRequest.of(pageNum, pageSize, new Sort(Sort.Direction.ASC, "proId"));
        if (grade == null) {
            return  problemPageRepo.findAll(pageable);
        }
        return  problemPageRepo.findByGrade(grade,pageable);
    }

    /**
     * 批量删除
     * @param list
     */
    public void batchDeletion(List<Integer> list){
        problemRepo.batchDeletion(list);
    }

    /**
     * 根据proId删除题目，单选删除
     * @param proId
     */
    public void deleteProblem(Integer proId){
        problemRepo.deleteById(proId);
    }
}
